package com.itheima.controller;

import com.itheima.domain.Book;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;

@Controller
public class BookController {
    //使用原生request对象传递参数
    @RequestMapping("/showPageAndData1")
    public String showPageAndData1(HttpServletRequest request) {
        request.setAttribute("name","Hello SpringMVC!");
        System.out.println("user mvc controller is running ...");
        return "page";

    }
    //使用Model形参传递参数
    @RequestMapping("/showPageAndData2")
    public String showPageAndData2(Model model) {
        model.addAttribute("name", "Java");

        Book book = new Book();
        book.setName("SpringMVC入门实战");
        book.setPrice(66.66d);
        model.addAttribute("book", book);
        return "page";
    }

    //使用void形参传递参数，该对象还封装了页面信息
    @RequestMapping("/showPageAndData3")
    public ModelAndView showPageAndData3(ModelAndView modelAndView) {
        // 也可以手动创建对象，替换形参中参数
        // ModelAndView modelAndView = new ModelAndView();
        Book book = new Book();
        book.setName("SpringMVC入门实战");
        book.setPrice(66.66d);

        modelAndView.addObject("book",book);

        // 添加数据方式，key对value
        modelAndView.addObject("name","Jock");
        // 设置页面方式，该方法最后一次执行结果生效
        modelAndView.setViewName("page");
        // // 返回值设定成ModelAndView对象
        return modelAndView;

    }

    //void对象支持转发的手工设定，该设定不会启用前缀后缀的页面拼接格式
    @RequestMapping("/showPageAndData4")
    public ModelAndView showPageAndData4(ModelAndView modelAndView) {
        modelAndView.setViewName("forward:/WEB-INF/page/page.jsp");
        return modelAndView;
    }

    //void对象支持重定向的手工设定，该设定不会启用前缀后缀的页面拼接格式
    @RequestMapping("/showPageAndData5")
    public ModelAndView showPageAndData6(ModelAndView modelAndView) {
        modelAndView.setViewName("redirect:page.jsp");
        return modelAndView;
    }
}
